//
//  DomainClient.m
//  Proto
//
//  Created by kingly on 15/11/22.
//  Copyright © 2015年 Will. All rights reserved.
//

#import "DomainClient.h"

#import "NSObject+MKBlockTimer.h"
#import "ProtocolBuffers.h"
#import "Person.pb.h"
#import "People.pb.h"

#import "NSData+DataToHexString.h"

#import "SocketManager.h"


@interface DomainClient ()<SocketManagerDelegate>{

     NSMutableData* cachServiceData;   //发送的数据包
}
@end

@implementation DomainClient

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)connectToUrl:(NSURL *)url;
{
    SocketManager *socket = [SocketManager getInstance];
    socket.myDelegate = self;
    [socket connectToUrl:url];
}

- (IBAction)send:(id)sender;
{
    //序列化
    People_Builder  *testbuilder = [People builder];
    [testbuilder setName:@"libin"];
    [testbuilder setId:123456];
    [testbuilder setEmail:@"libin@163.com"];
    
    People *people1 = [testbuilder build];
    NSData *sendData= [NSData dataWithData:people1.data];
    
    DataPacket *packet = [[DataPacket alloc] init];
    packet.protoDataContent = sendData;
    [[SocketManager getInstance] sendContent:packet];
  
}
#pragma mark - SocketManagerDelegate
/**
 *  socket 读取数据
 */
-(void) socketDidReadData:(NSData *)data{
    
    NSData *receivedData  = [NSData dataWithData:data];
    if (receivedData != nil) {
        People *people2 = [People parseFromData:receivedData];
        NSLog(@"[Client 反序列化 ] Received Data: ： %@ id: %d",people2.name, people2.id);
    }
    
}

/**
 * socket 连接失败或有误
 */
-(void) socketDidDisconnectWithError:(NSError *)error{

    NSLog(@"[Client]  connection error: %@", error);
}



@end
